<template>

<div class="login" align="center">
    <el-card style="width: 480px;">
    <template #header>
      <div class="card-header">
        <div style="text-align: center;">
            <span>登录</span>
        </div>
       
      </div>
    </template>

    <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
    status-icon
  >
    <el-form-item label="账号" prop="userName">
      <el-input v-model="ruleForm.userName" />
    </el-form-item>
    <el-form-item label="密码" prop="userPwd">
      <el-input v-model="ruleForm.userPwd" type="password" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        登录
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
    </el-form-item>
  </el-form>
    
  </el-card>
</div>



</template>

<script setup lang="ts">
import { ElMessage, affixProps } from 'element-plus';
import {ref,reactive,onMounted} from 'vue'
import {useRouter} from 'vue-router'
const router=useRouter()
import axios from 'axios'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import {useCounterStore} from '../stores/counter'
const store=useCounterStore()

const form=reactive({
  userId:store.$state.userId,
  userNickname:store.$state.userNickname,
  

})
interface RuleForm {
  userName: string
  userPwd: string

  
}

const ruleFormRef = ref<FormInstance>()

const ruleForm = reactive<RuleForm>({
  userName: '',
  userPwd: '',
  
})


const rules = reactive<FormRules<RuleForm>>({
  userName: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 10, message: '账号在3-10位之间', trigger: 'blur' },
  ],
  userPwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max:10, message: '密码在6-10位之间', trigger: 'blur' },
  ]
})

const login=()=>{
    axios.get('http://localhost:11836/FarmApp/Login',{
        params:{
            userName:ruleForm.userName,
            userPwd:ruleForm.userPwd
        }
    })
    .then(res=>{
        if(res.data.length>0)
        {
            ElMessage.success('登录成功')
            store.$state.userId=res.data[0].userId
            store.$state.userNickname=res.data[0].userNickname
            store.$state.isLogin=true
            router.push('/goodsList')
            console.log(res.data)
        }
        else
        {
            ElMessage.error('用户名或密码错误')
        }
    })
}

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      login()
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}


</script>

<style>
.login{
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url(https://tse1-mm.cn.bing.net/th/id/OIP-C.XNL6tI-_DBEVZCB1AbaWiwHaFj?w=221&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7);
}

.el-button
{
    justify-content: center;
    align-items: center;
    display: flex;
}

</style>